/*
 * @Date: 2024-07-09 17:40:59
 * @Description: 项目入口
 */

import './src/core/$'
import React, { Component, memo, useEffect } from 'react'
// import { Provider, useDispatch } from 'react-redux'
import SplashScreen from 'react-native-splash-screen'

// import { actions, store, useSelector } from './store'
// import codePush from 'react-native-code-push'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { PortalRootProvider } from '@/components/PortalRoot'
// import notifee, { EventType } from '@notifee/react-native'
import { AppState, NativeEventSubscription, Platform, Pressable, Text } from 'react-native'
import { KeyboardProvider } from 'react-native-keyboard-controller'
import Router from '@/core/router'
import { createStyle } from '@/core/styleSheet'

if (__DEV__) {
  // 关闭promise异常的黄色警告
  const oldWarn = console.warn
  console.warn = (msg) => {
    if (msg.indexOf('Possible Unhandled Promise Rejection') !== -1) {
      oldWarn(msg)
    }
  }
}

// const HotTag = () => {
//   const codePush = useSelector((state) => state.storage.codePush)
//   return (
//     codePush?.label &&
//     codePush?.deploymentKey === Platform.select(TEST_CODEPUSH_KEY) && (
//       <Pressable style={[styles.container]}>
//         <Text style={styles.text}>{codePush.label}</Text>
//       </Pressable>
//     )
//   )
// }

const styles = createStyle({
  container: {
    position: 'absolute',
    top: 32,
    left: 10,
    backgroundColor: 'rgba(0, 0, 0, 0.5)',
    width: 80,
    height: 30,
    borderRadius: 15,
    justifyContent: 'center',
    alignItems: 'center',
    pointerEvents: 'none',
    zIndex: 10,
  },
  text: {
    fontSize: 20,
    color: 'white',
    fontWeight: 'bold',
  },
})

const App = memo(() => {

  useEffect(() => {
    let appState: NativeEventSubscription
    return () => {
      appState?.remove?.()
    }
  }, [])

  return (
    <PortalRootProvider>
      {/* <ModalProvider> */}
        <Router />
        {/* <HotTag /> */}
      {/* </ModalProvider> */}
    </PortalRootProvider>
  )
})

class RunApp extends Component {
  isCodePush(): boolean {
    return !__DEV__
  }

  // 如果有更新的提示
  // syncImmediate() {
  //   codePush.sync(
  //     {
  //       installMode: codePush.InstallMode.IMMEDIATE,
  //       mandatoryInstallMode: codePush.InstallMode.IMMEDIATE,
  //       updateDialog: null,
  //     },
  //     (status) => {
  //       console.log(status, codePush.SyncStatus)
  //     },
  //     (progress) => {
  //       console.log('CodePush progress:', progress)
  //     }
  //   )
  // }
  // componentWillMount() {
  //   if (this.isCodePush()) {
  //     codePush.disallowRestart() // 禁止重启
  //     this.syncImmediate() // 开始检查更新
  //   }
  // }

  componentDidMount() {
    if ($.isAndroid) {
      setTimeout(() => {
        SplashScreen.hide()
      }, 300)
    }
    // if (this.isCodePush()) {
    //   codePush.allowRestart() // 在加载完了，允许重启
    // }
  }

  render() {
    return (
      <SafeAreaProvider>
        <KeyboardProvider>
          {/* <Provider store={store}> */}
            <App {...this.props} />
          {/* </Provider> */}
        </KeyboardProvider>
      </SafeAreaProvider>
    )
  }
}

export default RunApp
